﻿using App.Authorization.Roles;
using App.Authorization.Users;
using App.Configuration;
using App.MultiTenancy;
using System.Collections.Generic;
using System.Linq;

namespace App.EntityFramework.Seed
{
    internal class DefaultTenantUserAndRoleCreator
    {
        private readonly WebPlusDbContext _context;

        public DefaultTenantUserAndRoleCreator(WebPlusDbContext _context)
        {
            this._context = _context;
        }

        internal void Create()
        {
            //Default tenant
            var defaultTenant = _context.Tenant.FirstOrDefault(t => t.TenancyName == Tenant.DefaultTenantName);
            if (defaultTenant == null)
            {
                defaultTenant = new Tenant(Tenant.DefaultTenantName);

                _context.Tenant.Add(defaultTenant);
                _context.SaveChanges();
            }

            var defaultUser = _context.User.FirstOrDefault(t => t.UserName == User.DefaultUserName && t.TenantId == defaultTenant.Id);

            if (defaultUser == null)
            {
                var allPermissions = _context.MenuPermission.Where(s => !s.IsHostTenant).ToList();

                var rolePermissions = new List<RolePermission>();
                allPermissions.ForEach(delegate (MenuPermission permission)
                {
                    rolePermissions.Add(new RolePermission
                    {
                        MenuId = permission.Id,
                        TenantId = defaultTenant.Id
                    });
                });

                var roles = new List<Role> {
                    new Role {
                        RoleName = "Manager",
                        RoleDesc = "管理员",
                        Permissions = rolePermissions,
                        TenantId = defaultTenant.Id
                    },
                    new Role
                    {
                        RoleName = "User",
                        RoleDesc = "普通用户",
                        TenantId = defaultTenant.Id
                    }
                };

                foreach (var role in roles)
                {
                    if (_context.Role.Any(s => s.RoleName == role.RoleName && s.TenantId == defaultTenant.Id))
                    {
                        break;
                    }

                    _context.Role.Add(role);
                    _context.SaveChanges();
                }

                if (!_context.User.Any(s => s.UserName == User.DefaultUserName && s.TenantId == defaultTenant.Id))
                {
                    var userRoles = new List<UserRole>();
                    foreach (var item in roles)
                    {
                        userRoles.Add(new UserRole
                        {
                            RoleId = item.Id,
                            TenantId = defaultTenant.Id
                        });
                    }
                    var user = new User
                    {
                        UserName = User.DefaultUserName,
                        PasswordHash = "1000:XagbFPKkV97cOsEmHIP9nffok3US64A8:+05tjUFHcQHdpE088kGm27HoCoZRpf3W",  //123456
                        Roles = userRoles,
                        IsActive = true,
                        TenantId = defaultTenant.Id
                    };

                    _context.User.Add(user);
                    _context.SaveChanges();
                }
            }
        }
    }
}